package com.zxkj.utils;

import org.apache.commons.lang3.StringEscapeUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2017/7/11.
 */
public class ReadHtmlHelper {

    public static String GetRawValue(String raw, String name) {
        raw = raw.replace("\n", "");
        String value = null;
        Pattern p = Pattern.compile("<td[^>]*>" + name + " </td><td>(?<value>((?!</td>).)*)</td>");
        Matcher m = p.matcher(raw);//开始编译
        while (m.find()) {
            value = m.group("value");
            break;
        }
        value = StringEscapeUtils.unescapeHtml4(value);
        return value;
    }

    public static String GetRawValue2(String raw, String name) {
        raw = raw.replace("\n", "");
        String value = null;
        Pattern p = Pattern.compile("<td[^>]*><span class=\"FR_label\">" + name + "</span></td><td>(?<value>((?!</span></td>).)*)</span></td>");
        Matcher m = p.matcher(raw);//开始编译
        while (m.find()) {
            value = m.group("value");
            break;
        }
        value = StringEscapeUtils.unescapeHtml4(value);
        return value;
    }

    public static List<String> GetRawValues(String raw, String name, String split) {
        List<String> list;
        String value = GetRawValue(raw, name);
        if (value != null) {
            list = new ArrayList<>(Arrays.asList(value.split(split)));
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).length() == 0) {
                    list.remove(i);
                    i--;
                    continue;
                }
                list.set(i, CommonUtils.trimAndRegex(list.get(i)));
            }
        } else {
            list = new ArrayList<>();
        }
        return list;
    }
}
